const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../services/user.js');
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    spinShow: false,
    form: {
      name: '',
      companyName: '',
      phoneNumber: '',
      email: ''
    },
    lockLoad:false,//是否锁定 onshow不加载
  },
  getIndexData() {
    let that = this;
    util.request(api.GET_USER_MINIPROGRAM, {}, 'POST', 'application/json').then(res => {
      if (res.success == true) {
        let data = new Object();
        data.form = {
          name: res.result.name,
          companyName: res.result.companyName,
          phoneNumber: res.result.phoneNumber,
          email: res.result.email,
        }
        that.setData(data);
      } else {
        util.$Message({
          content: res.message,
          type: 'error'
        });
      }
    });
    if(that.data.lockLoad){
      that.setData({
        lockLoad: false,
      })
    }
    
  },
  //授权手机号码回调函数
  getPhoneNumber(e) {
    
    let that = this;
    that.setData({
      lockLoad: true,
    })
    util.request(api.AuthParsePhoneNumber, e.detail, 'POST', 'application/json').then(res => {
      if (res.success == true) {
        let data = new Object();;
        data[`form.phoneNumber`] = res.result.phoneNumber;
        that.setData(data);
      } else {
        util.$Message({
          content: res.message,
          type: 'error'
        });

      }
    });
  },
  //input框输入监听事件
  input: function (e) {
    let data = new Object();
    let field = e.target.dataset.field;
    data[`form.${field}`] = e.detail.value;
    this.setData(data);
  },
  //提交表单
  submit() {

    let that = this;
    let form = that.data.form;
    let data = new Object();
    for (let o in form) {
      data[`form.${o}`] = util.trim(form[o]);
    }
    that.setData(data);
    if (util.isNotEmpty(data['form.email']) && util.regexEmail(data['form.email'])) {
      util.$Message({
        content: '电子邮箱格式不正确',
        type: 'error'
      });
      return;
    }
    that.setData({ spinShow: true })
    util.request(api.UPDATE_USER_MINIPROGRAM, that.data.form).then(res => {
      that.setData({ spinShow: false })
      if (res.success == true) {
        util.$Message({
          content: '提交成功',
          type: 'success'
        });
      } else {
        util.$Message({
          content: res.message,
          type: 'error'
        });

      }
    });


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!this.data.lockLoad){
      this.getIndexData();
    }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})